using System;
using System.Windows.Forms;
using Comteck.Dto.Goods;
using Comteck.Ktt.Services.Goods;
using Comteck.Linq;
using Comteck.Localization;
using Comteck.Winforms;
using Comteck.Winforms.Forms;
using DevExpress.XtraBars;
using ExpressMapper.Extensions;
using Microsoft.Extensions.DependencyInjection;

namespace Comteck.Ktt.WinUI.Goods {
  /// <summary>
  /// 商品管理
  /// </summary>
  public partial class ProductAdmin : FrmBase {
    private readonly IProductService _productService;
    private CKObservableCollection<ProductDto> mainDataList;

    /// <summary>
    /// 
    /// </summary>
    public ProductAdmin(IProductService _productService) {
      this._productService = _productService;
      InitializeComponent();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void ProductAdmin_Load(object sender, EventArgs e) {
      if (this.DesignMode) {
        return;
      }

      // page
      ucDataNavigator1.barPageSize.EditValue = 100;
      ucDataNavigator1.UcDataNavigatorPaged += () => {
        btnSearch.PerformClick();
      };

      gcMain.DataSource = mainDataList = new CKObservableCollection<ProductDto>(this);

      colCreateTime.FormatDate();
    }

    private async void btnSearch_ItemClick(object sender, ItemClickEventArgs e) {
      // search model
      var searchModel = new ProductQuery() {
        Name = searchName.EditValue.To<string>(),
        Page = 1,
      };

      // 分页
      if (ucDataNavigator1.GetPageData(searchModel) == false) {
        return;
      }

      // search
      ajaxLoad.Show();
      var result = await _productService.GetPageListAsync(searchModel);
      ajaxLoad.Hide();

      // search result
      if (result.Code == 1) {
        result.Data.MapTo(mainDataList, x => x.Id, x => x.Id);
        ucDataNavigator1.InitPageData(result);
      } else {
        mainDataList.Clear();
        this.ShowMsg(BaseResources.Search_Failed_Reason, result.Message);
      }

    }

    private void searchName_KeyDown(object sender, KeyEventArgs e) {
      if (e.KeyCode == Keys.Enter) {
        btnSearch.PerformClick();
      }
    }

    private void btnAdd_ItemClick(object sender, ItemClickEventArgs e) {
      var form = SysGlobal.ServiceProvider.GetRequiredService<ProductAction>();
      form.AddOrUpdateAction = (entity) => gvMain.BindAfterInsert<ProductDto>(entity);
      form.Owner = this;
      form.ShowDialog();
    }

    private void btnEdit_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is ProductDto model)) {
        return;
      }

      var form = SysGlobal.ServiceProvider.GetRequiredService<ProductAction>();
      form.EntityId = model.Id;
      form.AddOrUpdateAction = (entity) => gvMain.BindAfterUpdate(model, entity);
      form.Owner = this;
      form.ShowDialog();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private async void btnDelete_ItemClick(object sender, ItemClickEventArgs e) {
      if (!(gvMain.GetFocusedRow() is ProductDto model)) {
        return;
      }

      var dialog = this.ShowMsg(BaseResources.Delete_Selected_Confirm, "提示", MessageBoxButtons.YesNo,
        MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
      if (dialog == DialogResult.No) {
        return;
      }

      ajaxLoad.Show();
      try {
        await _productService.DeleteAsync(model.Id);
        this.ShowMsg(BaseResources.Delete_Success);
        mainDataList.Remove(model);
      } catch (Exception ex) {
        this.ShowMsg(BaseResources.Delete_Failed_Reason, ex.Message);
      }
      ajaxLoad.Hide();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnExport_ItemClick(object sender, ItemClickEventArgs e) {
      gvMain.ExportDialog();
    }

    private void btnClose_ItemClick(object sender, ItemClickEventArgs e) {
      this.Close();
    }
  }
}
